package dhm.com.hpx.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.ExtensionAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.GetExtensionListBean;
import dhm.com.hpx.utils.SpUtils;

public class ExtensionActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.fabu_name)
    TextView fabu_name;
    @BindView(R.id.text_like)
    TextView textLike;
    @BindView(R.id.line_like)
    TextView lineLike;
    @BindView(R.id.text_liked)
    TextView textLiked;
    @BindView(R.id.line_liked)
    TextView lineLiked;
    @BindView(R.id.recy)
    XRecyclerView recy;

    int page=1;
    String type="1";
    private String uid;

    private PressenterImpl pressenter;
    private List<GetExtensionListBean.DataBean> beanList;
    private ExtensionAdapter adapter;
    private int position;

    @Override
    protected int getLayout() {
        return R.layout.activity_extension;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        type = getIntent().getStringExtra("type");

        uid = SpUtils.getString(this, "uid");

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        recy.setPullRefreshEnabled(true);
        recy.setLoadingMoreEnabled(true);
        adapter = new ExtensionAdapter(ExtensionActivity.this);
        adapter.setdeleteClick(new ExtensionAdapter.OnClick() {
            @Override
            public void item(int posttion) {
                Intent intent;
                switch (ExtensionActivity.this.type){
                    case "1":
                        intent=new Intent(ExtensionActivity.this,ExtensionarticleActivity.class);
                        intent.putExtra("extension_id",beanList.get(posttion).getExtension_id()+"");
                        startActivity(intent);
                        break;
                    case "2":
                        intent=new Intent(ExtensionActivity.this,ExtensionradioActivity.class);
                        intent.putExtra("extension_id",beanList.get(posttion).getExtension_id()+"");
                        startActivity(intent);
                        break;
                    default:
                }
            }
        });

        adapter.setaddClick(new ExtensionAdapter.OnIntentClick() {
            @Override
            public void item(int positions) {
                position = positions;
                Map<String,String> map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("uid",uid);
                map.put("extension_id",beanList.get(positions).getExtension_id()+"");
                pressenter.sendMessage(ExtensionActivity.this,Constant.del_extension,map, Bean.class);

            }
        });

        recy.setAdapter(adapter);
        switch (type){
            case "1":
                title.setText("文章推广");
                fabu_name.setText("发布文章");
                page=1;
                textLiked.setTextColor(getResources().getColor(R.color.main));
                textLike.setTextColor(Color.parseColor("#666666"));
                lineLiked.setVisibility(View.VISIBLE);
                lineLike.setVisibility(View.INVISIBLE);
                getArticle();
                break;
            case "2":
                title.setText("视频推广");
                fabu_name.setText("发布视频");
                page=1;
                textLike.setTextColor(getResources().getColor(R.color.main));
                textLiked.setTextColor(Color.parseColor("#666666"));
                lineLike.setVisibility(View.VISIBLE);
                lineLiked.setVisibility(View.INVISIBLE);
                getRadio();
                break;
            default:
        }

    }

    private void getArticle() {
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getArticle();
            }

            @Override
            public void onLoadMore() {
                getArticle();
            }
        });

        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("page",page+"");
        map.put("type",type);
        pressenter.sendMessage(this,Constant.extensionlist,map, GetExtensionListBean.class);
    }

    private void getRadio() {
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getRadio();
            }

            @Override
            public void onLoadMore() {
                getRadio();
            }
        });
        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("page",page+"");
        map.put("type",type);
        pressenter.sendMessage(this,Constant.extensionlist,map, GetExtensionListBean.class);
    }

    @Override
    protected void initData() {

    }
    @SuppressLint("NewApi")
    @OnClick({R.id.back, R.id.video, R.id.article, R.id.todaa})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.video:
                type="2";
                textLike.setTextColor(getResources().getColor(R.color.main));
                textLiked.setTextColor(Color.parseColor("#666666"));
                lineLike.setVisibility(View.VISIBLE);
                lineLiked.setVisibility(View.INVISIBLE);
                page=1;
                adapter.setList(new ArrayList<GetExtensionListBean.DataBean>());
                getRadio();
                break;
            case R.id.article:
                type="1";
                textLiked.setTextColor(getResources().getColor(R.color.main));
                textLike.setTextColor(Color.parseColor("#666666"));
                lineLiked.setVisibility(View.VISIBLE);
                lineLike.setVisibility(View.INVISIBLE);
                page=1;
                adapter.setList(new ArrayList<GetExtensionListBean.DataBean>());
                getArticle();
                break;
            case R.id.todaa:
                Intent intent=new Intent(this,AddextensionActivity.class);
                intent.putExtra("type",type);
                startActivity(intent );
                break;
                default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetExtensionListBean){
            GetExtensionListBean getExtensionListBean= (GetExtensionListBean) data;
            if (getExtensionListBean.getCode()==1){
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    beanList = getExtensionListBean.getData();
                }else {
                    beanList.addAll(getExtensionListBean.getData());
                }
                adapter.setList(beanList);
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
            }
            recy.loadMoreComplete();
            recy.refreshComplete();
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            if (bean.getCode()==1){
                beanList.remove(position);
                adapter.setList(beanList);
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            }


        }
    }

    @Override
    public void fail(String error) {

    }
}
